package org.yudidi.properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.yudidi.service.FooService;

import javax.annotation.PostConstruct;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 在这种绑定中，getter 和 setter 方法是强制的，因为这里的绑定是通过标准的Java Bean属性绑定，但是也有例外，这里不讲，在POJO中，getter 和 setter 方法一般都会写上的吧。具体见 spring boot 文档第 24.7小节
 */
@ConfigurationProperties(prefix = "foo")
public class FooProperties {
    private static final Logger logger = LoggerFactory.getLogger(FooProperties.class);

    /***
     * 这个注解在这里没实际用处，就是为了方便在属性配置类构造完成后打印日志，看看配置信息是否加载到本对象中.
     */
    @PostConstruct
    public void valueInjectTest() {
        logger.error("属性配置类构造函数执行后："+toString());
    }

    private boolean enabled;

    private InetAddress remoteAddress;

    private Security security;

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public InetAddress getRemoteAddress() {
        return remoteAddress;
    }

    public void setRemoteAddress(InetAddress remoteAddress) {
        this.remoteAddress = remoteAddress;
    }

    public Security getSecurity() {
        return security;
    }

    public void setSecurity(Security security) {
        this.security = security;
    }

    /**
     * 级联属性
     */
    public static class Security {

        private String username;

        private String password;

        private List<String> roles = new ArrayList<>(Collections.singleton("USER"));

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public List<String> getRoles() {
            return roles;
        }

        public void setRoles(List<String> roles) {
            this.roles = roles;
        }

        @Override
        public String toString() {
            return "{ " + "username='" + username + '\'' + ", password='" + password + '\'' + " }";
        }
    }

    @Override
    public String toString() {
        return "{ " + "enabled=" + enabled + ", remoteAddress=" + remoteAddress + ", security="
                + security + " }";
    }
}
